/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.lis.ctrl;



import com.cxhd.lis.model.newmodel.LisUnit;
import static com.cxhd.lis.model.newmodel.LisUnit.lisUnitdao;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.util.List;


/**
 *
 * @author cxhd
 */
@ControllerBind(controllerKey = "/view/lis/unit")
public class LisUnitCtrl extends Controller{
     public void index(){
        render("/view/lis/unit.jsp");
    }
          public void getUnit() {
//         renderJson(Db.use("mysql").find("select PK,KSDM,KSMC,SRM1 from lis_ksdm"));
     String sql="select PK,UNITNAME,UNITCHINA ";
     String param=" from lis_unit where UNITNAME like '%"+getPara("searchContent")+"%' or UNITCHINA like '%"+getPara("searchContent")+"%'";
Page<Record> userPage = Db.use("mysql").paginate(getParaToInt("currentPage",getParaToInt("currentPage")), getParaToInt("pageSize",getParaToInt("pageSize")), sql, param);
renderJson(userPage);
         
    }

      public void checkUnit() {
//         Record re=Db.use("mysql").findFirst("select KSDM from lis_ksdm where KSDM="+getPara("ksdm"));
          List<Record> list= Db.use("mysql").find("select * from lis_unit where UNITNAME=?",getPara("UNITNAME"));
       if(list.size()!=0){ 
            if(getPara("PK")!=""&&getPara("PK")!=null){//判断是新增还是修改
                if(Integer.valueOf(getPara("PK"))==list.get(0).get("PK")){ //修改是允许修改（本身主键和查询到的主键相同）
                     renderText("false");
                }else{
                   renderText("true");  
                }
            }else{
                renderText("true");
            }
        
        }else{
            renderText("false");
        }
    }
      public void saveUnit(){
        try {
           new LisUnit().saveUnit(this);
            renderText("true");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }
      }
      public void updateUnit(){
          try {
           lisUnitdao.updateUnit(this);
            renderText("true");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }
      }
      public void deleteUnit(){
        try {
           new LisUnit().deleteUnit(Integer.valueOf(getPara("PK")));
          renderText("true");
        } catch (Exception ex) {
            ex.printStackTrace();
            renderText("false");
            
        }
      }
}
